package com.atguigu.tingshu.user.mq;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.user.context.UpdateUserPaidRecordStrategyContext;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.strategy.UpdateUserPaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * Author  王东剑
 * Created on 24/6/2024 下午7:55
 * Package_name com.atguigu.tingshu.user.mq
 * Description
 */
@Component
@Slf4j
public class UserReceiver {
	@Autowired
	private UserInfoService userInfoService;
	@KafkaListener(topics = KafkaConstant.QUEUE_USER_PAY_RECORD)
	public void updateUserPaidRecord(ConsumerRecord<String,String> record){
		String userPaidRecordVoJSON = record.value();
		if(!StringUtils.isEmpty(userPaidRecordVoJSON)){
			UserPaidRecordVo userPaidRecordVo = JSON.parseObject(userPaidRecordVoJSON, UserPaidRecordVo.class);
			userInfoService.updateUserPaidRecord(userPaidRecordVo);

			log.info("更新用户支付记录: {}", JSON.toJSONString(userPaidRecordVo));
		}

	}
}
